//
//  BaseObject.swift
//  Student
//
//  Created by zhuxuhong on 2017/9/29.
//  Copyright © 2017年 北大方正电子. All rights reserved.
//

import UIKit

typealias JSONDictionary = [String: Any]

class BaseObject: NSObject {
	override func setValue(_ value: Any?, forUndefinedKey key: String) {
		print("[\(self)-UndefinedKey]: \(key)")
	}
	
	/*
	fileprivate class func showErrorToast(_ message: String, callback: Network.Failure?){
		Tipper.toast(message: message)
		callback?(message)
	}
	
	public static func request(api: API, success: @escaping Network.Success, failure: Network.Failure?)
	{
		guard !Network.isNetworkNotReachable else {
			failure?("网络连接断开")
			return
		}
		if api.needsTipper {
			Tipper.hud(style: .loading, message: "正在请求数据")
		}
		Network.request(url: api.url, method: api.method, encoding: api.encoding, parameters: api.parameters) { (error, data) in
			guard 
				error == nil else{
				if api.needsTipper{
					showErrorToast("数据请求失败，请稍后重试", callback: failure)
				}
				return
			}
			guard
				let _ = data, 
				let jsonObject = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? JSONDictionary, 
				let json = jsonObject else{
				if api.needsTipper{
					showErrorToast("数据请求失败，请稍后重试", callback: failure)
				}
				return
			}
			success(json)
		}
	}*/
}

extension NSObject{
	public static func model<T: NSObject>(with dictionary: [String: Any]) -> T{
		let model = T()
		model.setValuesForKeys(dictionary)
		return model
	}
}
